$(function () {
    $("#jqGrid").jqGrid({
    	url: baseURL + 'client/capital/list',
        datatype: "json",
        colModel: [			
        	{ label: 'id', name: 'id', key: true,width:70,align:'center' },
			{ label: '用户名', name: 'userName',index:"user_name",width:100,align:'center' },
			{ label: '手机号', name: 'mobileNo',index:"mobile_no" ,width:100,align:'center' },
			{ label: '押金金额', name: 'depositAmount', index:"deposit_amount",width:80 ,align:'center'},
			{ label: '押金充值时间', name: 'createTime',index:"create_time",width:150 ,align:'center' },
			{ label: '流水号', name: 'outTradeNo',index:"out_trade_no" ,width:300 ,align:'center'},
			{ label: '退款时间', name: 'modifyTime',index:"modify_time",width:150 ,align:'center'},
			{ label: '状态', name: 'auditStatus', index:"audit_status", width: 80 ,align:'center',
				formatter:function(value,options,rowData){
					if(value==0){
						return "新注册";
					}if(value==2){
						return "正常";
					}else if(value==6){
						return "已退押金";
					}else if(value==7){
						return "退款失败";
					}else if(value==10){
						return "欠费";
					}else if(value==11){
						return "严重欠费";
					}else {
						return "资金状态不正常";
					}
				}
			,sortable:false},
			{ label: '余额', name: 'cashAmount',index:"cash_amount",width:80,align:'center'},
			{ label: '资金流水', name: 'bookImg',index:"book_img",width:70,align:'center',formatter:function(cellvalue, options, rowObject){  
				return "<a  onclick=\"getInfosRecord("+rowObject.id+")\">查看</a>";
            }},
			/*{ label: '操作', name: 'createTime',index:"create_time" ,align:'center'},
			{ label: '最后登录时间', name: 'modifyTime',index:"modify_time",align:'center'},
			{ label: '评论数量', name: 'commentCount',index:"comment_count",width:100,align:'center' },
			{ label: '报修次数', name: 'repairsNum',index:"repairs_num",width:100 ,align:'center',
				formatter:function(cellvalue, options, rowObject){
					return '<a onclick=\"getMaintainCount('+rowObject.id+')\">'+rowObject.repairsNum+'</a>'
				}
			,sortable:false},*/
			{ label: '操作', name: 'id', width: 120 ,
				formatter:function(cellvalue,options,rowObject){
	            	/*if(rowObject.auditStatus==1){//退款待审核，
	            		return "<a onclick=\"auditRefundDeposit("+rowObject.id+")\" >审核退款</a>"
	            	}else if(rowObject.auditStatus==2){
	            		return "<a onclick=\"update("+rowObject.id+")\" >审核拒绝</a>"
	            	}else if(rowObject.auditStatus==3){//
	            		return "<a onclick=\"operateRefundDeposit("+rowObject.id+")\" >操作退款</a>"
	            	}else if(rowObject.auditStatus==4){
	            		return "<a  onclick=\"getInfosRecord("+rowObject.id+")\">查看退款记录</a>"
	            	}else if(rowObject.auditStatus==5){
	            		return "退款失败";
	            	}
	            	else if(rowObject.auditStatus==6){
	            		return "<a onclick=\"addDepositAmount("+rowObject.id+")\" >充押金</a>"
	            	}else{
	            	<a onclick=\"applyForRefund("+rowObject.id+")\">申请退款</a>
	            	*/
	            		return "<a onclick=\"addCashAmount("+rowObject.id+")\" >充值</a>   "
	            	/*}*/
    			}
    		,sortable:false,align:'center'
    		}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50,100,200],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        autoScroll:true,
        shrinkToFit:false,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        }
        
    });
    /*创建jqGrid的操作按钮容器*/
    /*可以控制界面上增删改查的按钮是否显示*/
    //jQuery("#jqGrid").jqGrid('navGrid', '#jqGridPager', {edit : true,add : true,del : true});
    //表格宽度自适应
    $(function(){
       $(window).resize(function(){
          $("#jqGrid").setGridWidth($(window).width());
       });
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userName: null,
			mobileNo: null,
			auditStatus:null,
			refundStartTime:null,
			refundEndTime:null
        },
		showList: true,
		title: null,
		marketBook: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		getInfosRecord:function (id) {
			layer.open({
				type : 2,
				title : '用户资金流水',
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '930px', '580px' ],
				content : './client-capital-record.html?id='+id
			});
		},
		applyForRefund: function(id){
			var url =  "client/capital/applyForRefund";
			var data = {"id":id};
			ajaxPost(url,data,'申请退款成功');
		},
		auditRefundDeposit: function(id){
			var url =  "client/capital/auditRefundDeposit";
			var data = {"id":id};
			ajaxPost(url,data,'审核退款成功');
		},
		addDepositAmount: function(id){
			
			layer.confirm('冲押金:<br/><input id="depositAmount" type="text" value=\"'+'\" />', {
				btn : [ '提交', '取消' ]
					//按钮
			}, function() {
				var depositAmount = document.getElementById("depositAmount").value;
				var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
				if(!reg.test(depositAmount)){
					layer.msg("请输入正确金额。");
					return;
				}
				var url =  "client/capital/addDepositAmount";
				var data = {"userId":id,"depositAmount":depositAmount};
				ajaxPost(url,data,"押金充值成功");
				layer.close(layer.index);
			}, function() {
				
			});
		},
		addCashAmount: function(user_id){
			layer.confirm('冲余额:<br/><input id="cashAmount" type="text" value=\"'+'\" />', {
				btn : [ '提交', '取消' ]
					//按钮
			}, function() {
				var cashAmount = document.getElementById("cashAmount").value;
				var reg = /(^\-?[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
				if(!reg.test(cashAmount)){
					layer.msg("请输入正确金额。");
					return;
				}
				var url =  "client/capital/addCashAmount";
				var data = {"userId":user_id,"cashAmount":cashAmount};
				ajaxPost(url,data,"余额充值成功");
				layer.close(layer.index);
			}, function() {
				
			});
		},
		operateRefundDeposit: function(user_id){
			var url =  "client/capital/operateRefundDeposit";
			var data = {"id":user_id};
			ajaxPost(url,data,'操作退款成功');
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData : {
					'userName' : vm.q.userName,
					'mobileNo' : vm.q.mobileNo,
					'auditStatus' : vm.q.auditStatus,
					'refundStartTime':vm.q.refundStartTime,
					'refundEndTime':vm.q.refundEndTime,
					'payStartTime':vm.q.payStartTime,
					'payEndTime':vm.q.payEndTime
				},
				page : page
			}).trigger("reloadGrid");
		}
	}
});